import { Component, OnInit, ViewChild } from '@angular/core';
import { IrRecordListComponent } from '../scheduled-event/ir-record-list/ir-record-list.component';
import { IrInfoDetailsComponent } from '../scheduled-event/ir-info-details/ir-info-details.component';
import { NzMessageService } from 'ng-zorro-antd';
import { ScheduledService } from './scheduled.service';
import { Subscription } from 'rxjs';
import { BullySubjectService } from 'app/shared/service';
import { filter } from 'rxjs/operators';
import { SYSTEM_EVENT } from 'app/app.constants';
import { EventCate } from './scheduled-event.interface';
import { PublicService } from 'app/public/public.service';
import { SELECT_OPTIONS } from 'app/app.constants';

@Component({
    selector: 'itmp-scheduled-event',
    templateUrl: './scheduled-event.component.html',
    styleUrls: ['./scheduled-event.component.css']
})
export class ScheduledEventComponent implements OnInit {
    @ViewChild(IrRecordListComponent) private _IRListCom: IrRecordListComponent;
    @ViewChild(IrInfoDetailsComponent) private childcomponent: IrInfoDetailsComponent;
    index = 0;
    zoneList = [];
    isIRclicked = false;
    isEdit = false;
    tempIrRecord = [];
    subscription: Subscription = null;
    data: Array<EventCate> = [];
    accessButton = {
        Create: false,
        Edit: false,
        Delete: false,
        GIS: false,
        export: false,
        PRINT: false,
        Look: false
    };
    statusData: Array<EventCate> = [];

    constructor(
        private message: NzMessageService,
        private SEservice: ScheduledService,
        private bully: BullySubjectService,
        private publicService: PublicService
    ) {}

    ngOnInit() {
        // 接受地图发来的设备创建消息
        this._createScheduledEventHandle();
        this.getAllEnumInfoData();
        this.getButtonAccess();
        this.bully.zoneListBehavior.subscribe(res => {
            this.zoneList = res['selectZoneList'] ? res['selectZoneList'] : [];
        });
    }

    getButtonAccess() {
        this.accessButton = this.publicService.getButtonAccess('33', this.accessButton);
    }

    // 获取Enum列表接口
    getAllEnumInfoData() {
        // this.SEservice.getAllEnumInfo().subscribe(res => {
        //     if (res) {
        //         this.data = res.data;
        //     }
        // });
        const options = this.publicService.getSelectOptionsList(SELECT_OPTIONS.EVENT_TYPE, true);
        if (options) {
            this.data = [...options.data];
        }
        const options2 = this.publicService.getSelectOptionsList(SELECT_OPTIONS.EVENT_STATUS, true);
        if (options2) {
            this.statusData = [...options2.data].sort((a, b) => a.value - b.value);
        }
    }

    // tslint:disable-next-line:use-life-cycle-interface
    ngOnDestroy(): void {
        this.bully.resetBSubject();
        if (this.subscription) {
            this.bully.resetBSubject();
            this.subscription.unsubscribe();
        }
    }

    tabChange(e) {
        this.isIRclicked = true;
        this.index = 2;
        setTimeout(() => {
            if (this.childcomponent) {
                this.childcomponent.getSelectionList();
                this.childcomponent.initIrInfoDetail(e);
                this.childcomponent.editForm();
            }
        });
    }

    clickEdit(e) {
        this.isEdit = e;
    }

    clearDetail() {
        this.childcomponent.initIrInfoDetail(this.tempIrRecord);
    }

    irSelected(e) {
        this.isIRclicked = true;
        this.tempIrRecord = e;
        // setTimeout(() => {
        //     if (this._IRResCom) {
        //         this._IRResCom['getData'](e);
        //     }
        // });
    }

    cancelEdit(e) {
        this.index = 0; // 回到list页面
        this._IRListCom.ngOnInit(); // 调用list页面的刷新方法
        this.isIRclicked = false; //  关闭detail选项卡
        this.clickEdit(false);
    }

    private _createScheduledEventHandle() {
        this.subscription = this.bully
            .getBSubject()
            .pipe(filter(res => !!res))
            .subscribe(res => {
                if (res['type'] === SYSTEM_EVENT.CREATE_EVENT) {
                    const data = res['data'];
                    this.index = 0;
                    setTimeout(() => {
                        if (this._IRListCom) {
                            this._IRListCom.changeTabPage.emit({
                                end_Link_ID: res['fromGis'] ? null : data['linkID'],
                                end_X_Coor: res['fromGis'] ? null : data['longitude'],
                                end_Y_Coor: res['fromGis'] ? null : data['latitude'],
                                eventId: null,
                                lane_Blockage: null,
                                location_SubType: null,
                                location_Type: null,
                                planEndTime: res['fromGis'] ? null : new Date(),
                                planStartTime: res['fromGis'] ? null : new Date(),
                                remark: null,
                                road_Name: data['roadName'],
                                start_Link_ID: data['linkID'],
                                start_X_Coor: data['longitude'],
                                start_Y_Coor: data['latitude'],
                                type: null,
                                veri_State: null,
                                zone_ID: data['zoneID'],
                                fromGis: res['fromGis']
                            });
                        }
                    }, 1000);
                }
            });
    }
}
